home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / net / parnet-3.2 / extras / parpc / amigafiles / sources / unit_str.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  4KB  |  182 lines

  1.  
  2. /*
  3.  *  UNIT_STR.C            STREAM PROTOCOL (not impl yet)
  4.  *
  5.  *                currently just datagram proto.
  6.  */
  7.  
  8. #include "defs.h"
  9.  
  10. void StreamBeginIO(Iob *);
  11. void StreamAbortIO(Iob *);
  12. void StreamClose(Iob *);
  13. void StreamData(int, Packet *, long);
  14.  
  15. void UnitStreamOpen(Iob *, long, long);
  16.  
  17. /*
  18.  *  Called under Forbid
  19.  */
  20.  
  21. void
  22. UnitStreamOpen(iob, unitnum, flags)
  23. Iob *iob;
  24. long unitnum;
  25. long flags;
  26. {
  27.     Unit *unit;
  28.  
  29.     if (unit = FindUnitForPort(iob->io_Port)) {
  30.     if (unit->BeginIO != StreamBeginIO) {
  31.         iob->io_Error = PPERR_PORT_IN_USE;
  32.         return;
  33.     }
  34.     } else {
  35.     unit = AllocUnit(iob, StreamBeginIO, StreamAbortIO, StreamData, StreamClose);
  36.     }
  37.     iob->io_Unit = unit;
  38.     ++unit->RefCnt;
  39. }
  40.  
  41.  
  42. void
  43. StreamClose(iob)
  44. Iob *iob;
  45. {
  46.     Unit *unit = iob->io_Unit;
  47.  
  48.     if (--unit->RefCnt == 0) {
  49.     FreeUnit(unit);
  50.     }
  51.     iob->io_Unit = NULL;
  52. }
  53.  
  54. /*
  55.  *  UnitStreamData() is called whenever a low level network operation
  56.  *            completes for the given unit.  cmd is:
  57.  *
  58.  *            'r'     received data packet
  59.  *            'w'     wrote data packet
  60.  *            'W'     timeout writing data packet
  61.  *
  62.  *            The routine is called with the unit locked.
  63.  */
  64.  
  65. void
  66. StreamData(cmd, packet, actual)
  67. int cmd;
  68. Packet *packet;
  69. long actual;    /*  'w', 'W'    */
  70. {
  71.     Unit *unit = packet->io_Unit;
  72.     Iob *iob;
  73.     long n;
  74.  
  75.     switch(cmd) {
  76.     case 'r':
  77.     sprintf(StickyPort->DebugBuf, " RDSTR %d",actual);
  78.  
  79.     if (iob = (Iob *)RemHead(&unit->PendIOR)) {
  80.         iob->io_Flags &= ~IOF_QUEUED;
  81.  
  82.         n = actual;
  83.         iob->io_Actual = actual;
  84.         if (n < 0)
  85.         n = 0;
  86.         if (n > iob->io_Length) {
  87.         n = iob->io_Length;
  88.         iob->io_Error = PPERR_WARN_OVFLOW;
  89.         }
  90.         movmem((char *)packet->Data1, (char *)iob->io_Data, n);
  91.         if ((iob->io_Flags & IOF_QUICK) == 0)
  92.         ReplyMsg(&iob->io_Message);
  93.     }
  94.  
  95.     /*
  96.      *  Note that if no read requests are pending the datagram is
  97.      *  thrown away.
  98.      */
  99.  
  100.     FreeParPacket(packet);
  101.     break;
  102.     case 'W':
  103.     sprintf(StickyPort->DebugBuf, " TMSTR %d",actual);
  104.  
  105.     Remove(packet->iob);
  106.     iob->io_Flags &= ~IOF_QUEUED;
  107.     iob->io_Error = 1;
  108.     iob->io_Actual = actual;
  109.     if ((iob->io_Flags & IOF_QUICK) == 0)
  110.         ReplyMsg(&iob->io_Message);
  111.     FreeParPacket(packet);
  112.     break;
  113.     case 'w':
  114.     sprintf(StickyPort->DebugBuf, " WRSTR %d",actual);
  115.  
  116.     Remove(packet->iob);
  117.     iob->io_Flags &= ~IOF_QUEUED;
  118.     iob->io_Actual = actual;
  119.     if ((iob->io_Flags & IOF_QUICK) == 0)
  120.         ReplyMsg(&iob->io_Message);
  121.     FreeParPacket(packet);
  122.     break;
  123.     }
  124. }
  125.  
  126. void
  127. StreamBeginIO(iob)
  128. Iob *iob;
  129. {
  130.     Unit *unit = iob->io_Unit;
  131.     Packet *packet;
  132.  
  133.     iob->io_Error = 0;
  134.     iob->io_Actual = 0;
  135.     iob->io_Message.mn_Node.ln_Type = NT_MESSAGE;
  136.  
  137.     switch(iob->io_Command) {
  138.     case CMD_READ:
  139.     LockAddr(unit->UnitLock);
  140.     iob->io_Flags &= ~IOF_QUICK;
  141.     iob->io_Flags |= IOF_QUEUED;
  142.     AddTail(&unit->PendIOR, iob);
  143.     UnlockAddr(unit->UnitLock);
  144.     return;
  145.     case CMD_WRITE:
  146.     packet = AllocParPacket(iob, unit, iob->io_Data, iob->io_Length, NULL, 0);
  147.     LockAddr(unit->UnitLock);
  148.     AddTail(&unit->PendIOW, iob);
  149.     UnlockAddr(unit->UnitLock);
  150.  
  151.     QueuePacketForWrite(packet);
  152.     return;
  153.     default:
  154.     CtlBeginIO(iob);
  155.     return;
  156.     }
  157. }
  158.  
  159. /*
  160.  *  Abort a read or write request.  Currently only read requests may be
  161.  *  aborted.
  162.  */
  163.  
  164. void
  165. StreamAbortIO(iob)
  166. Iob *iob;
  167. {
  168.     if (iob->io_Command == CMD_READ) {
  169.     LockAddr(iob->io_Unit->UnitLock);
  170.     if ((iob->io_Flags & (IOF_QUEUED|IOF_RUN)) == IOF_QUEUED) {
  171.         Remove(iob);
  172.         UnlockAddr(iob->io_Unit->UnitLock);
  173.         iob->io_Flags &= ~IOF_QUEUED;
  174.         iob->io_Error = -1;     /*    ??? */
  175.         ReplyMsg(&iob->io_Message);
  176.     } else {
  177.         UnlockAddr(iob->io_Unit->UnitLock);
  178.     }
  179.     }
  180. }
  181.  
  182.